package com.oceansoft.mobile.biz.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.common.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("mapapi")
public class ApiMapController {
    static final String SZMAPCATEGORYURL = "http://testapi.sz-map.com/test/poi/category";

    /**
     * 调用苏州地图网接口
     * 搜索附近公安驻地、派出所、警务室
     *
     * @param request
     * @return
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "categorySearch")
    public Result geZhuDi(HttpServletRequest request) throws IllegalArgumentException, IOException {
        String categoryId = request.getParameter("categoryId");
        String geometry = request.getParameter("geometry");
        String coding = "0";
        String distance = "3000";
        String encoded = "false";
        String index = "0";
        String length = "10";

        String a = HttpClientUtil.doGet(SZMAPCATEGORYURL, "categoryId", categoryId, "coding", coding, "distance", distance, "encoded", encoded, "geometry", geometry, "index", index, "length", length);
        JSONObject json = JSONObject.parseObject(a);
        Result r = new Result();
        if ((Boolean) json.get("success")) {
            r.setSucc(Result.SUCC);
            // re.setMsg(json.get("msg").toString());
            r.setData(json.get("poiInfo"));
        } else {
            r.setSucc(Result.FAILURE);
            r.setMsg(json.get("errorMsg").toString());
            String code = json.get("errorCode").toString();
            r.setStatusCode(Integer.parseInt(code));
        }
        return r;
    }

}
